#include "utility.h"
#include "eventHandlerTask.h"
#include "monitorTimer.h"

#include "spi/configure_device.h"
#include "spi/spi_driver.h"
#include "dma/dma.h"
#include "gpio/gwGpio.h"
#include "i2c/i2c_driver.h"

#define mainUART_COMMAND_CONSOLE_STACK_SIZE	( configMINIMAL_STACK_SIZE * 2UL )
#define mainUART_COMMAND_CONSOLE_TASK_PRIORITY	( configMAX_PRIORITIES - 2 )

int main( void )
{
	xil_printf( " \r\n************ Start Freertos example with gpio main **************\r\n" );

	init_state();

	/* eventHandler */
	QueueHandle_t xEventQueue = NULL;
	xEventQueue = xQueueCreate(100, sizeof(Event) );
	configASSERT( xEventQueue );

	int Status;
	Status = XSpi_init();
	if (Status != XST_SUCCESS) {
		xil_printf("Failed XSpi_init\r\n");
		return XST_FAILURE;
	}

	spi_read(XPAR_AXI_SPI_BASEADDR,0x810500, HMC7044_SPI_SS);

	// Status = init_axidma(xEventQueue);
	// if (Status != XST_SUCCESS) {
	// 	xil_printf("Failed init_axidma\r\n");
	// 	return XST_FAILURE;
	// }

	Status = InitHwIcap();
	if (Status != XST_SUCCESS) {
		xil_printf("Failed InitHwIcap\r\n");
		return XST_FAILURE;
	}

	Status = init_gpio();
	if (Status != XST_SUCCESS) {
		xil_printf("Failed init_gpio\r\n");
		return XST_FAILURE;
	}

	Status = IIC_axi_initialize(IIC_DEVICE_ID);
	if (Status != XST_SUCCESS) {
		xil_printf("Failed IIC_axi_initialize\r\n");
		return XST_FAILURE;
	}
	reset_gpio(O_SFP_RESETL);

	// for( ;; )
	// {
	// 	if(spi_configure() == 0){
	// 		break;
	// 	}
	// }

	// return;

	createEventHandlerTask(xEventQueue); // task1

	createSpiConfigureTask(xEventQueue); // task2 , exit

#ifndef configEnableLog
	vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
	/* Register the standard CLI commands. */
	vRegisterSampleCLICommands();
#endif

	vTaskStartScheduler();

	xil_printf(" End of Main()!\r\n ");
	for( ;; );
}



